using System;
using System.Collections.Generic;
using System.Linq;
using WeChatCmsCommon.EnumBusiness;
using WeChatDataAccess;
using WeChatModel.DatabaseModel;

namespace WeChatService
{
    /// <summary>
    /// 人员服务
    /// </summary>
    public class PersonService
    {
        /// <summary>
        /// 数据服务
        /// </summary>
        private PersonData _dataAccess = new PersonData();
        /// <summary>
        /// 根据项目ID获取人员列表
        /// </summary>
        /// <param name="projectId">项目ID</param>
        /// <param name="userId">用户ID</param>
        /// <returns></returns>
        public List<PersonModel> GetListByProjectId(long projectId, long userId)
        {
            return _dataAccess.GetListByProjectId(projectId, userId);
        }

        /// <summary>
        /// 根据ID获取人员
        /// </summary>
        /// <param name="id">人员ID</param>
        /// <returns></returns>
        public PersonModel Get(long id)
        {
            if (id < 1) return null;
            var data = _dataAccess.Get(id);
            if (data == null || data.IsDel == FlagEnum.HadOne) return null;
            return data;
        }

        /// <summary>
        /// 根据姓名获取人员
        /// </summary>
        /// <param name="personName">人员姓名</param>
        /// <param name="userId">用户ID</param>
        /// <returns></returns>
        public PersonModel GetByName(string personName, long userId)
        {
            return _dataAccess.GetByName(personName, userId);
        }

        /// <summary>
        /// 获取无项目的人员列表（ProjectId=0）
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns></returns>
        public List<PersonModel> GetUnassignedPersons(long userId)
        {
            return _dataAccess.GetUnassignedPersons(userId);
        }

        /// <summary>
        /// 获取所有人员列表
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="tagIds">标签ID列表（可选，用于筛选）</param>
        /// <returns></returns>
        public List<PersonModel> GetAllPersons(long userId, List<long> tagIds = null)
        {
            return _dataAccess.GetAllPersons(userId, tagIds);
        }

        /// <summary>
        /// 添加人员
        /// </summary>
        /// <param name="model">人员模型</param>
        /// <returns>人员ID</returns>
        public long Add(PersonModel model)
        {
            if (model == null) return 0;
            model.Id = 0; // 确保是添加操作
            return _dataAccess.SaveModel(model);
        }

        /// <summary>
        /// 更新人员
        /// </summary>
        /// <param name="model">人员模型</param>
        /// <returns></returns>
        public bool Update(PersonModel model)
        {
            if (model == null || model.Id < 1) return false;
            var result = _dataAccess.SaveModel(model);
            return result > 0;
        }

        /// <summary>
        /// 检查人员姓名是否存在
        /// </summary>
        /// <param name="personName">人员姓名</param>
        /// <param name="userId">用户ID</param>
        /// <param name="excludeId">排除的人员ID（编辑时使用）</param>
        /// <returns></returns>
        public bool IsPersonNameExists(string personName, long userId, long? excludeId = null)
        {
            return _dataAccess.IsPersonNameExists(personName, userId, excludeId);
        }

        /// <summary>
        /// 检查人员姓名是否在指定项目中存在
        /// </summary>
        /// <param name="personName">人员姓名</param>
        /// <param name="userId">用户ID</param>
        /// <param name="projectId">项目ID，0表示检查无项目的人员</param>
        /// <param name="excludeId">排除的人员ID（编辑时使用）</param>
        /// <returns></returns>
        public bool IsPersonNameExistsInProject(string personName, long userId, long projectId, long? excludeId = null)
        {
            return _dataAccess.IsPersonNameExistsInProject(personName, userId, projectId, excludeId);
        }

        /// <summary>
        /// 清空指定项目下所有人员的项目ID
        /// </summary>
        /// <param name="projectId">项目ID</param>
        /// <param name="userId">用户ID</param>
        public void ClearProjectIdByProjectId(long projectId, long userId)
        {
            _dataAccess.ClearProjectIdByProjectId(projectId, userId);
        }

        /// <summary>
        /// 删除人员
        /// </summary>
        /// <param name="id">人员ID</param>
        public void DelModel(long id)
        {
            _dataAccess.DelModel(id);
        }

        /// <summary>
        /// 保存人员信息
        /// </summary>
        /// <param name="model">人员模型</param>
        /// <returns></returns>
        public long SaveModel(PersonModel model)
        {
            if (model == null) return 0;
            return _dataAccess.SaveModel(model);
        }
    }
}